package com.lvcoding.sec3.secdemo3.controller;

import com.lvcoding.sec3.secdemo3.validatecode.parent.ValidateCodeProcessor;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.ServletWebRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 验证码接口
 */
@Api(tags = "验证码接口类")
@RestController
@RequestMapping("code")
public class ValidateCodeController {

    @Autowired
    private Map<String , ValidateCodeProcessor> validateCodeGenerators;

    /**
     * 验证码接口
     * type=sms -> 短信验证码
     * type=img -> 图片验证码
     * @param request
     * @throws ServletRequestBindingException
     */
    @ApiOperation("验证码接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name="mobile",value="如果type是sms时必传",dataTypeClass = String.class, paramType = "query",example="12345"),
    })
    @GetMapping("{type}")
    public void createSmsCode(HttpServletRequest request, HttpServletResponse response, @PathVariable("type")String type) throws Exception {
        validateCodeGenerators.get(type + "CodeProcessor").create(new ServletWebRequest(request, response));
    }
}
